package com.jshare.service.Impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.jshare.dao.MemberDAO;
import com.jshare.entity.Member;
import com.jshare.service.MemberService;
import com.jshare.util.Pager;

@Service("memberService")
public class MemberServiceImpl implements MemberService {

	@Resource(name="memberDao")
	private MemberDAO memberDao;
	
	public MemberDAO getMemberDao() {
		return memberDao;
	}
	public void setMemberDao(MemberDAO memberDao) {
		this.memberDao = memberDao;
	}
	public boolean login(Member member) throws Exception {
		// TODO Auto-generated method stub
		Member loadMember = memberDao.findById(member.getName());
		if(loadMember!=null)
		{
			if(loadMember.getPasswd().equals(member.getPasswd()))
			{
				return true;
			}
		}
		return false;
	}
	//*******自定义***/
	public String test() {
		// TODO Auto-generated method stub
		return "hello";
	}
	public Member loadMember(String id) {
		// TODO Auto-generated method stub
		return memberDao.findById(id);
	}
	public void addMember(Member member) {
		// TODO Auto-generated method stub
		memberDao.save(member);
	}
	@Override
	public List loadMemberByExample(Member member) {
		// TODO Auto-generated method stub
		return memberDao.findByExample(member);
	}
	@Override
	public List<Member> loadAll() {
		// TODO Auto-generated method stub
		return memberDao.findAll();
	}
	@Override
	public void deleteMember(String id) {
		// TODO Auto-generated method stub
		memberDao.delete(memberDao.findById(id));
	}
	@Override
	public Pager getPageData(int pageSize, int pageNow) {
		// TODO Auto-generated method stub
		String sql = "from Member";
		int rowCount = memberDao.getTotalRows("select count(*) from Member as member");
		Pager pager = new Pager(rowCount);
		pager.setPagenow(pageNow);
		if(pageSize <= 0 )
		{
			pageSize = 5;
		}
		pager.setPageSize(pageSize);
		pager.setData(memberDao.getPage(sql,(pager.getPagenow()-1)*pager.getPageSize(),pager.getPageSize()));
		return pager;
	}
	

}
